package com.jstyle.jclifexd.daoManager;

import android.text.TextUtils;

import com.jstyle.jclifexd.dao.TemperatureHistoryDataDao;
import com.jstyle.jclifexd.model.TemperatureHistoryData;
import com.jstyle.jclifexd.utils.DbManager;
import com.jstyle.jclifexd.utils.SharedPreferenceUtils;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/7/9.
 */

public class TempHistoryDataDaoManager {
    public static void insertData(TemperatureHistoryData TemperatureHistoryData) {
        DbManager.getInstance().getDaoSession().getTemperatureHistoryDataDao().insertOrReplace(TemperatureHistoryData);
    }

    public static void insertData(List<TemperatureHistoryData> TemperatureHistoryDataList) {
        DbManager.getInstance().getDaoSession().getTemperatureHistoryDataDao().insertOrReplaceInTx(TemperatureHistoryDataList);
    }

    public static List<TemperatureHistoryData> queryData(String date) {

        return queryData(date, date);
    }

    public static List<TemperatureHistoryData> queryAllData() {
        QueryBuilder<TemperatureHistoryData> queryBuilder = DbManager.getInstance().getDaoSession().getTemperatureHistoryDataDao().queryBuilder();

        return queryBuilder.list();
    }

    public static List<TemperatureHistoryData> queryAllDataByAddress() {
        List<TemperatureHistoryData> TemperatureHistoryDataList = new ArrayList<>();
        String address = SharedPreferenceUtils.getSpString(SharedPreferenceUtils.KEY_ADDRESS);
        if (TextUtils.isEmpty(address)) return TemperatureHistoryDataList;
        QueryBuilder<TemperatureHistoryData> queryBuilder = DbManager.getInstance().getDaoSession().getTemperatureHistoryDataDao().queryBuilder();
        return queryBuilder.where(TemperatureHistoryDataDao.Properties.Address.eq(address)).list();
    }

    public static List<TemperatureHistoryData> queryData(String startDate, String endDate) {//睡眠从当天12点到第二天12点
        List<TemperatureHistoryData> TemperatureHistoryDataList = new ArrayList<>();
        String address = SharedPreferenceUtils.getSpString(SharedPreferenceUtils.KEY_ADDRESS);
        if (TextUtils.isEmpty(startDate) || TextUtils.isEmpty(endDate) || TextUtils.isEmpty(address))
            return TemperatureHistoryDataList;
        QueryBuilder<TemperatureHistoryData> queryBuilder = DbManager.getInstance().getDaoSession().getTemperatureHistoryDataDao().queryBuilder();
        TemperatureHistoryDataList = queryBuilder.where(TemperatureHistoryDataDao.Properties.Address.eq(address), TemperatureHistoryDataDao.Properties.Time.between(startDate + " 12:00:00", endDate + " 12:00:00")).orderAsc(TemperatureHistoryDataDao.Properties.Time).list();
        return TemperatureHistoryDataList;
    }

    public static List<TemperatureHistoryData> queryExerciseData(String startDate, String endDate) {
        List<TemperatureHistoryData> TemperatureHistoryDataList = new ArrayList<>();
        String address = SharedPreferenceUtils.getSpString(SharedPreferenceUtils.KEY_ADDRESS);
        if (TextUtils.isEmpty(startDate) || TextUtils.isEmpty(endDate) || TextUtils.isEmpty(address))
            return TemperatureHistoryDataList;
        QueryBuilder<TemperatureHistoryData> queryBuilder = DbManager.getInstance().getDaoSession().getTemperatureHistoryDataDao().queryBuilder();
        TemperatureHistoryDataList = queryBuilder.where(TemperatureHistoryDataDao.Properties.Address.eq(address), TemperatureHistoryDataDao.Properties.Time.between(startDate + " 00:00:00", endDate + " 23:59:00")).orderAsc(TemperatureHistoryDataDao.Properties.Time).list();
        return TemperatureHistoryDataList;
    }

    public static List<TemperatureHistoryData> queryGpsData(String startDate, String endDate) {
        List<TemperatureHistoryData> TemperatureHistoryDataList = new ArrayList<>();
        String address = SharedPreferenceUtils.getSpString(SharedPreferenceUtils.KEY_ADDRESS);
        ;
        if (TextUtils.isEmpty(startDate) || TextUtils.isEmpty(endDate) || TextUtils.isEmpty(address))
            return TemperatureHistoryDataList;
        QueryBuilder<TemperatureHistoryData> queryBuilder = DbManager.getInstance().getDaoSession().getTemperatureHistoryDataDao().queryBuilder();
        TemperatureHistoryDataList = queryBuilder.where(TemperatureHistoryDataDao.Properties.Address.eq(address), TemperatureHistoryDataDao.Properties.Time.between(startDate, endDate)).orderAsc(TemperatureHistoryDataDao.Properties.Time).list();
        return TemperatureHistoryDataList;
    }

    public static List<TemperatureHistoryData> queryDayData(String queryDate) {
        List<TemperatureHistoryData> TemperatureHistoryDataList = new ArrayList<>();
        String address = SharedPreferenceUtils.getSpString(SharedPreferenceUtils.KEY_ADDRESS);
        if (TextUtils.isEmpty(queryDate) || TextUtils.isEmpty(address))
            return TemperatureHistoryDataList;
        QueryBuilder<TemperatureHistoryData> queryBuilder = DbManager.getInstance().getDaoSession().getTemperatureHistoryDataDao().queryBuilder();
        TemperatureHistoryDataList = queryBuilder.where(TemperatureHistoryDataDao.Properties.Address.eq(address), TemperatureHistoryDataDao.Properties.Time.between(queryDate + " 00:00:00", queryDate + " 23:59:59")).orderAsc(TemperatureHistoryDataDao.Properties.Time).list();
        return TemperatureHistoryDataList;
    }

    public static List<TemperatureHistoryData> queryYearData(String queryDate, String endDate) {
        List<TemperatureHistoryData> TemperatureHistoryDataList = new ArrayList<>();
        String address = SharedPreferenceUtils.getSpString(SharedPreferenceUtils.KEY_ADDRESS);
        if (TextUtils.isEmpty(queryDate) || TextUtils.isEmpty(endDate) || TextUtils.isEmpty(address))
            return TemperatureHistoryDataList;
        QueryBuilder<TemperatureHistoryData> queryBuilder = DbManager.getInstance().getDaoSession().getTemperatureHistoryDataDao().queryBuilder();
        TemperatureHistoryDataList = queryBuilder.where(TemperatureHistoryDataDao.Properties.Address.eq(address), TemperatureHistoryDataDao.Properties.Time.between(queryDate + " 00:00:00", endDate + " 23:59:59")).orderAsc(TemperatureHistoryDataDao.Properties.Time).list();
        return TemperatureHistoryDataList;
    }

    public static TemperatureHistoryData getLastTemperatureHistoryData() {
        QueryBuilder<TemperatureHistoryData> queryBuilder = DbManager.getInstance().getDaoSession().getTemperatureHistoryDataDao().queryBuilder();
        String address = SharedPreferenceUtils.getSpString(SharedPreferenceUtils.KEY_ADDRESS);
        if (TextUtils.isEmpty(address)) return null;
        return queryBuilder.where(TemperatureHistoryDataDao.Properties.Address.eq(address)).list().size() == 0 ? null : queryBuilder.where(TemperatureHistoryDataDao.Properties.Address.eq(address)).orderDesc(TemperatureHistoryDataDao.Properties.Time).list().get(0);
    }

    public static String getLastStaticTemperatureHistoryDataTime() {
        QueryBuilder<TemperatureHistoryData> queryBuilder = DbManager.getInstance().getDaoSession().getTemperatureHistoryDataDao().queryBuilder();
        String address = SharedPreferenceUtils.getSpString(SharedPreferenceUtils.KEY_ADDRESS);
        if (TextUtils.isEmpty(address)) return null;
        List<TemperatureHistoryData> queryBuilderList = queryBuilder.where(TemperatureHistoryDataDao.Properties.Address.eq(address), TemperatureHistoryDataDao.Properties.Mode.eq(0)).orderDesc(TemperatureHistoryDataDao.Properties.Time).list();
        return queryBuilderList.size() == 0 ? null : queryBuilderList.get(0).getTime();
    }

    public static String getLastDymicTemperatureHistoryDataTime() {
        QueryBuilder<TemperatureHistoryData> queryBuilder = DbManager.getInstance().getDaoSession().getTemperatureHistoryDataDao().queryBuilder();
        String address = SharedPreferenceUtils.getSpString(SharedPreferenceUtils.KEY_ADDRESS);
        if (TextUtils.isEmpty(address)) return null;
        List<TemperatureHistoryData> queryBuilderList = queryBuilder.where(TemperatureHistoryDataDao.Properties.Address.eq(address), TemperatureHistoryDataDao.Properties.Mode.eq(1)).orderDesc(TemperatureHistoryDataDao.Properties.Time).list();
        return queryBuilderList.size() == 0 ? null : queryBuilderList.get(0).getTime();
    }

    public static void deleteAll() {
        DbManager.getInstance().getDaoSession().getTemperatureHistoryDataDao().deleteAll();
        ;
    }
}
